package prassein.rsm.web;

import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.common.utils.Utility;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.entity.base.ResumeFile;
import prassein.rsm.service.base.AuditManager;
import prassein.rsm.service.base.ResumeFileManager;
import prassein.rsm.service.base.ResumeManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name = "input",location="resumeReview.jsp"), 
	@Result(name = "success",location="resumeReview2.jsp"), 
	@Result(name = "error",location="login.jsp")})
public class ResumeReview extends ActionSupport {

	private static final long serialVersionUID = 6282357968297806250L;
	
	private int resumeId = -1;
	
	private int type;
	
	private int state;
	
	private int changeState;
	
	@Autowired
	private ResumeManager resumeManager;
	
	@Autowired
	private ResumeFileManager resumeFileManager;
	
	@Autowired
	private AuditManager auditManager;
	
	private Resume resume = new Resume();
	
	private int submitResult = -1;
	private String file1;
	private String file2;
	private String file3;
	private String file4;
	private String file1Loc;
	private String file2Loc;
	private String file3Loc;
	private String file4Loc;
	
	@Override 
	public String execute() throws Exception {
		cleanResumeFileInfo();
		submitResult = -1;
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		
		if (type == 0) {
			if (session.get(Constants.USERNAME) != null && !session.get(Constants.USERNAME).equals("")) {
				if (resumeId > -1) {
					resume = resumeManager.getResume(resumeId);
					initResumeFileInfo(resume);
					
				} else if (session.get(Constants.LATEST_EDIT_REUMEID) != null && !session.get(Constants.LATEST_EDIT_REUMEID).equals("")) {
					resumeId = Integer.parseInt(session.get(Constants.LATEST_EDIT_REUMEID).toString());
					resume = resumeManager.getResume(resumeId);
					initResumeFileInfo(resume);
					
				} else {
					resume = new Resume();
				}
				
				if (changeState == 1) {
					changeState(resume, state);
				}
				
				cleanState();
				return INPUT;
			} else {
				cleanState();
				return ERROR;
			}
		} else {
			if (session.get(Constants.USERNAME) != null && !session.get(Constants.USERNAME).equals("")) {

				if (resumeId > -1) {
					resume = resumeManager.getResume(resumeId);
					initResumeFileInfo(resume);
					
				} else if (session.get(Constants.LATEST_EDIT_REUMEID) != null && !session.get(Constants.LATEST_EDIT_REUMEID).equals("")) {
					resumeId = Integer.parseInt(session.get(Constants.LATEST_EDIT_REUMEID).toString());
					resume = resumeManager.getResume(resumeId);
					initResumeFileInfo(resume);
					
				} else {
					resume = new Resume();
				}
				
				if (changeState == 1) {
					changeState(resume, state);
				}
				cleanState();
				return SUCCESS;
			} else {
				cleanState();
				return ERROR;
			}
		}
	}
	
	private void changeState(Resume resume2, int state2) {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String stateString = "跟进中";
		if (state2 == 0) {
			stateString = "跟进中";
		} else {
			stateString = "关闭";
		}
		
		if (resume2.getState() == null) {
			resume2.setState(state2);
			resume2.setModifyTime(Utility.GetDate());
			auditManager.save(session.get(Constants.USERNAME).toString(), resume2.getName(), "修改状态为:" + stateString);
			resumeManager.save(resume2);
		} else if (resume2.getState() != state2) {
			resume2.setState(state2);
			resume2.setModifyTime(Utility.GetDate());
			auditManager.save(session.get(Constants.USERNAME).toString(), resume2.getName(), "修改状态为:" + stateString);
			resumeManager.save(resume2);
		}
	}

	private void cleanState() {
		//changeState = 0;
	}
	private void cleanResumeFileInfo() {
		file1 = "";
		file2 = "";
		file3 = "";
		file4 = "";
		
		file1Loc = "";
		file2Loc = "";
		file3Loc = "";
		file4Loc = "";
	}
	
	private void initResumeFileInfo(Resume resume) {
		if (resume == null) {
			return ;
		}
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		
		Integer file1ID = resume.getResumeFile1();
		Integer file2ID = resume.getResumeFile2();
		Integer file3ID = resume.getResumeFile3();
		Integer file4ID = resume.getResumeFile4();
		
		if (file1ID != null) {
			ResumeFile resFile1 = resumeFileManager.get(file1ID);
			if (resFile1 != null) {
				file1Loc = resFile1.getLocation();
				file1 = resFile1.getFileName();
				
				session.put("file1Loc", file1Loc);
			}
		}
		
		if (file2ID != null) {
			ResumeFile resFile2 = resumeFileManager.get(file2ID);
			if (resFile2 != null) {
				file2Loc = resFile2.getLocation();
				file2 = resFile2.getFileName();
			}
		}
		
		if (file3ID != null) {
			ResumeFile resFile3 = resumeFileManager.get(file3ID);
			if (resFile3 != null) {
				file3 = resFile3.getFileName();
				file3Loc = resFile3.getLocation();
			}
		}
		
		if (file4ID != null) {
			ResumeFile resFile4 = resumeFileManager.get(file4ID);
			if (resFile4 != null) {
				file4Loc = resFile4.getLocation();
				file4 = resFile4.getFileName();
			}
		}
	}
	
	public ResumeManager getResumeManager() {
		return resumeManager;
	}

	public void setResumeManager(ResumeManager resumeManager) {
		this.resumeManager = resumeManager;
	}

	public ResumeFileManager getResumeFileManager() {
		return resumeFileManager;
	}

	public void setResumeFileManager(ResumeFileManager resumeFileManager) {
		this.resumeFileManager = resumeFileManager;
	}

	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	public int getResumeId() {
		return resumeId;
	}

	public void setResumeId(int resumeId) {
		this.resumeId = resumeId;
	}

	public int getSubmitResult() {
		return submitResult;
	}

	public void setSubmitResult(int submitResult) {
		this.submitResult = submitResult;
	}

	public String getFile1() {
		return file1;
	}

	public void setFile1(String file1) {
		this.file1 = file1;
	}

	public String getFile2() {
		return file2;
	}

	public void setFile2(String file2) {
		this.file2 = file2;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getFile3() {
		return file3;
	}

	public void setFile3(String file3) {
		this.file3 = file3;
	}

	public String getFile4() {
		return file4;
	}

	public void setFile4(String file4) {
		this.file4 = file4;
	}

	public String getFile1Loc() {
		return file1Loc;
	}

	public void setFile1Loc(String file1Loc) {
		this.file1Loc = file1Loc;
	}

	public String getFile2Loc() {
		return file2Loc;
	}

	public void setFile2Loc(String file2Loc) {
		this.file2Loc = file2Loc;
	}

	public String getFile3Loc() {
		return file3Loc;
	}

	public void setFile3Loc(String file3Loc) {
		this.file3Loc = file3Loc;
	}

	public String getFile4Loc() {
		return file4Loc;
	}

	public void setFile4Loc(String file4Loc) {
		this.file4Loc = file4Loc;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getChangeState() {
		return changeState;
	}

	public void setChangeState(int changeState) {
		this.changeState = changeState;
	}

	public AuditManager getAuditManager() {
		return auditManager;
	}

	public void setAuditManager(AuditManager auditManager) {
		this.auditManager = auditManager;
	}
}
